package com.lingnan.controller;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lingnan.mapper.ActiveMapper;
import com.lingnan.pojo.Active;
import com.lingnan.util.Result;
import com.lingnan.util.ResultGenerator;
import com.lingnan.util.TencentCosUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.file.*;

/**
 * @author 刘宇浩
 */
@RestController
@RequestMapping("/file")
@Slf4j
public class FileController {

    @Autowired
    private ActiveMapper activeMapper;

    @Autowired
    private TencentCosUtils tencentCosUtils;

    @ApiOperation("下载文件到指定目录")
    @PostMapping("/download")
    public Result download(@RequestParam Integer id,@RequestParam @ApiParam("传文件输出位置") String outputFilePath){
        LambdaQueryWrapper<Active> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Active::getId, id);
        Active active = activeMapper.selectOne(wrapper);
        tencentCosUtils.download(active.getFileUrl(),outputFilePath);
        return ResultGenerator.genSuccessResult(200,"下载成功！");
    }

    @ApiOperation("获得文件的图床https全链接")
    @PostMapping("/downloadUrl")
    public Result downloadUrl(@RequestParam @ApiParam("传活动Id") Integer id){
        LambdaQueryWrapper<Active> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Active::getId, id);
        Active active = activeMapper.selectOne(wrapper);
        return ResultGenerator.genSuccessResult(200,active.getFileUrl(),"下载成功！");
    }


/*
    @ApiOperation("上传文件到指定目录")
    @PostMapping("/upload")
    public Result upload(@RequestParam @ApiParam("传全路径") String fileUrl,@RequestParam @ApiParam("传活动Id") Integer id){
        //对文件存入cos处理
        String fileName = tencentCosUtils.upload(fileUrl);
        //将文件路径更改成文件名称
        new


    }

*/

/*
    @GetMapping("/download")
    public void download (@RequestParam Integer id, HttpServletResponse response) throws IOException  {
        System.out.println("id:"  + id);
        LambdaQueryWrapper<Active> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Active::getId,id);
        Active active = activeMapper.selectOne(wrapper);
        System.out.println(active);
        //文件本地路径
        String fileUrl = active.getFileUrl();
        File file = new File(fileUrl);
        // 获取文件名
        String filename = file.getName();

        // 将文件写入输入流
        FileInputStream fileInputStream = new FileInputStream(file);
        //字节输入流
        InputStream fis = new BufferedInputStream(fileInputStream);
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();

        // 清空response
        response.reset();
        // 设置response的Header
        response.setCharacterEncoding("UTF-8");
        //Content-Disposition的作用：告知浏览器以何种方式显示响应返回的文件，用浏览器打开还是以附件的形式下载到本地保存
        //attachment表示以附件方式下载   inline表示在线打开   "Content-Disposition: inline; filename=文件名.mp3"
        // filename表示文件的默认名称，因为网络传输只支持URL编码的相关支付，因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
        // 告知浏览器文件的大小
        response.addHeader("Content-Length", "" + file.length());
        OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
        response.setContentType("application/octet-stream");
        outputStream.write(buffer);
        outputStream.flush();
    }
    */



}
